package com.chencs.app.config.service;

import com.alibaba.fastjson.JSON;
import com.chencs.app.AppCommon;
import com.chencs.app.config.mapper.AppConfigMapper;
import com.chencs.vo.biz.config.AdviseModel;
import com.chencs.vo.biz.config.ConfigModel;
import com.chencs.vo.biz.config.VersionModel;
import com.chencs.vo.net.request.AdviseRequest;
import com.chencs.vo.net.request.RequestBase;
import com.chencs.vo.net.response.ConfigResponse;
import com.chencs.vo.net.response.VersionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by fangzehua on 2017/7/12.
 */
@Service
@Transactional
public class AppConfigService {

    @Autowired
    private AppConfigMapper appConfigMapper;
    public VersionResponse checkVersion(RequestBase request){
        VersionResponse version = new VersionResponse();
        try {
            VersionModel versionModel = appConfigMapper.checkVersion(request.getVersion());
            if(versionModel!=null){
                if(versionModel.getVersion().equals(request.getVersion())){
                    version.setCode(AppCommon.NULL);
                    version.setMsg("当前已是最新版本");
                }else{
                    version.setVersion(versionModel);
                }

            }else{
                version.setCode(AppCommon.NULL);
                version.setMsg("找不到版本信息");
            }
        }catch (Exception e){
            e.printStackTrace();
            version.setCode(AppCommon.NULL);
        }
        return version;
    }


    public ConfigResponse getCofnig(RequestBase request){
        ConfigResponse config = new ConfigResponse();
        try {

            ConfigModel model = appConfigMapper.getConfig("");
             if(model!=null){
                 config.setConfigModel(model);
                 config.setCode(AppCommon.SUCCESS);
             }else{
                 config.setCode(AppCommon.NULL);
             }
        }catch (Exception e){
            e.printStackTrace();
            config.setCode(AppCommon.ERROR);
        }
        return config;
    }

    public ConfigResponse addAdvise(AdviseRequest request){
        ConfigResponse config = new ConfigResponse();
        try {
           appConfigMapper.addAdvise(request.getModel());
            config.setCode(AppCommon.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            config.setCode(AppCommon.ERROR);
        }
        return config;
    }


}
